package action;


import entity.DlbEntity;
import entity.XsbEntity;
import org.apache.struts2.ServletActionContext;
import service.DlbService;

import service.XsbService;
import utils.ResultUtils;
import utils.Singleton;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Lei
 * @create 2021-04-15 18:52
 */
public class LoginAction {

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


   

    private DlbService dlbService = (DlbService) Singleton.GetApplicationContext().getBean("DlbServiceImpl");
    private XsbService xsbService = (XsbService) Singleton.GetApplicationContext().getBean("XsbServiceImpl");


    /**
     * @author Lei
     * @description 登录的接口
     * @date 16:46 2021/4/16
     * @params []
     * @return java.lang.String
     */
    public String loginconrtoller() throws IOException {
        //根据id进行查询
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        DlbEntity dlbEntity = dlbService.selectByUserNameAndPassword(username, password);
        XsbEntity xsbEntity = xsbService.selectRecord(username);
        if (dlbEntity != null) {
            HttpServletRequest request=(HttpServletRequest) ServletActionContext.getRequest();
            HttpSession session=request.getSession();
            session.setAttribute("user",xsbEntity);
            stringObjectHashMap.put("code", 200);
            stringObjectHashMap.put("msg", "登录成功");
            ResultUtils.toJson(ServletActionContext.getResponse(), stringObjectHashMap);
            return "success";
        }
        stringObjectHashMap.put("code", 0);
        stringObjectHashMap.put("msg", "登录失败");
        ResultUtils.toJson(ServletActionContext.getResponse(), stringObjectHashMap);
        return "fail";
    }
    /**
     * @author Lei
     * @description 跳转到main的页面上去
     * @date 17:08 2021/4/16
     * @params []
     * @return java.lang.String
     */
    public String toMain() {
        return "main";
    }


   

}
